package cn.mw.cmdb.viewProcessor;

import cn.mw.microMonitorCommon.entity.cmdb.RelatedDataType;
import cn.mw.microMonitorCommon.entity.cmdb.TableHeaderItem;
import java.lang.reflect.Constructor;
import java.util.*;

public class ViewProcessorManage {
    private Map<String , Set<String>> fieldIdSetMap = new HashMap<>();

    private Map<String , Map<String, String>> fieldIdMapMap = new HashMap<>();;

    private static Map<RelatedDataType, Class> processorClassMap;

    public ViewProcessorManage(){
        if(null == processorClassMap){
            processorClassMap = new HashMap<>();
            //processorClassMap.put(RelatedDataType.dataAuth , DataAuthViewProcessor.class);
            processorClassMap.put(RelatedDataType.dataAuthUser , DataAuthMapViewProcessor.class);
            processorClassMap.put(RelatedDataType.dataAuthOrg , DataAuthMapViewProcessor.class);
            processorClassMap.put(RelatedDataType.dataAuthGroup , DataAuthMapViewProcessor.class);
            processorClassMap.put(RelatedDataType.modelGroup, ModelGroupViewProcessor.class);
            processorClassMap.put(RelatedDataType.model , ModelViewProcessor.class);
            processorClassMap.put(RelatedDataType.monitorServer , MonitorServerInfoProcessor.class);
            processorClassMap.put(RelatedDataType.pollingEngine , MonitorServerInfoProcessor.class);
        }
    }

    public void addFieldIdSet(String key, String value){
        Set<String> idSet = fieldIdSetMap.get(key);
        if(null == idSet){
            idSet = new HashSet<>();
            fieldIdSetMap.put(key ,idSet);
        }
        idSet.add(value);
    }

    public Set<String> getFieldIdSet(String key){
        return fieldIdSetMap.get(key);
    }

    public void addFieldIdMap(String key, String fieldId, String value){
        Map<String, String> fieldIdMap = fieldIdMapMap.get(key);
        if(null == fieldIdMap){
            fieldIdMap = new HashMap<>();
            fieldIdMapMap.put(key ,fieldIdMap);
        }
        fieldIdMap.put(fieldId ,value);
    }

    public Map<String, String> getFieldIdMap(String key){
        return fieldIdMapMap.get(key);
    }

    public Set<RelateDataViewProcessor> buildRelateDataViewProcessor(List<TableHeaderItem> tableHeaderItems) throws Exception{
        Set<RelateDataViewProcessor> ret = new HashSet<>();
        for(TableHeaderItem headerItem: tableHeaderItems){
            RelatedDataType relatedDataType = headerItem.getParseType();
            if(!RelatedDataType.none.equals(relatedDataType)){
                Class processorClass = processorClassMap.get(relatedDataType);
                if(null != processorClass){
                    Constructor constructor = processorClass.getConstructor(ViewProcessorManage.class ,List.class);
                    RelateDataViewProcessor relateDataViewProcessor = (RelateDataViewProcessor)constructor.newInstance(this ,tableHeaderItems);
                    ret.add(relateDataViewProcessor);
                }
            }
        }
        return ret;
    }

}
